<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { accountFormSchema } from './tableEmployeeData';
  // import { getDepartmentListResultModel } from '/@/api/baseInfo/departmentInfoAPI';
  import {
    DepartmentInfoParamsModel,
    // DepartmentInfoModel,
  } from '/@/api/baseInfo/model/DepartmentInfoModel';
  import { getEmployeeLeaves } from '/@/api/baseInfo/employeeLeaveAPI';
  import { EmployeeLeaveModel } from '/@/api/baseInfo/model/employeeLeave';
  import { saveEmployeeEntityVO } from '/@/api/baseInfo/employeeAPI';
  import { message } from 'ant-design-vue';

  export default defineComponent({
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      interface Option {
        label: string;
        value: string;
      }
      let employeeLeaves: EmployeeLeaveModel[];
      let employeeLeaveOptions: Option[] = [];
      // let deptnameoptionArr: Option[] = [];
      // let departmentInfoArr: DepartmentInfoModel[];
      const isUpdate = ref(true);
      const rowId = ref('');
      const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
        labelWidth: 100,
        baseColProps: { span: 24 },
        schemas: accountFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        DepartmentInfoParamsModel.departmentid = 1;

        // departmentInfoArr = toRaw(
        //   await getDepartmentListResultModel(DepartmentInfoParamsModel),
        // ).items;
        // for (let i = 0; i < departmentInfoArr.length; i++) {
        //   let deptnameOption: Option = { label: '', value: '' };
        //   deptnameOption.label = departmentInfoArr[i].departmentname;
        //   deptnameOption.value = departmentInfoArr[i].departmentname;
        //   deptnameoptionArr.push(deptnameOption);
        // }
        employeeLeaves = await getEmployeeLeaves();
        for (let i = 0; i < employeeLeaves.length; i++) {
          let employeeLeaveOption: Option = { label: '', value: '' };
          employeeLeaveOption.label = employeeLeaves[i].leaveTitle;
          employeeLeaveOption.value = employeeLeaves[i].leaveName;
          employeeLeaveOptions.push(employeeLeaveOption);
        }

        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
          rowId.value = data.record.id;
          setFieldsValue({
            ...data.record,
          });
        }
        updateSchema([
          // {
          //   field: 'deptName',
          //   componentProps: {
          //     options: deptnameoptionArr,
          //   },
          // },
          {
            field: 'levelName',
            componentProps: {
              options: employeeLeaveOptions,
            },
          },
        ]);
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });
          // TODO custom api

          if (values.certName === undefined) {
            values.certName = values.name;
          }
          await saveEmployeeEntityVO(values).then((result) => {
            if (result == 1) {
              message.info('员工信息保存成功');
              closeModal();
              emit('success');
            }
          });
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, getTitle, handleSubmit };
    },
  });
</script>
